<template>
<div>
    <el-card style="padding:10px;background:#EBEEF5">
    <div style="margin: 10px 0;width: 100%">
      <el-button type="primary" :icon="CirclePlus" @click="add">新增</el-button>
      <el-button type="danger" :icon="Remove" @click="Show" v-if="!isShow">批量删除</el-button>
      <el-button type="danger" :icon="Remove" @click="deleteBatch" v-if="isShow">删除</el-button>
      <el-button type="danger" :icon="CircleClose"  @click="Show" v-if="isShow">取消</el-button>
      <el-input v-model="name" placeholder="请输入菜单名" style="width: 15%;margin: 0 10px 0 50px" clearable/>
      <el-button type="primary" :icon="Search" style="margin-left: 10px" @click="load">查询</el-button>
      <el-button type="primary" :icon="Refresh" style="margin-left: 10px" @click="reset">重置</el-button>
    </div>
    <el-table :data="tableData"  :row-style="tableRowStyle" :cell-style="{'text-align':'center'}" :header-cell-style="{'text-align':'center'}"
              border resizable="true" style="width: 100%"  row-key="id" default-expand-all @selection-change="handleSelectionChange">
      <el-table-column v-if="isShow" type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" sortable/>
      <el-table-column prop="name" label="菜单名"/>
      <el-table-column prop="path" label="路径"/>
      <el-table-column prop="icon" label="图标"/>
      <el-table-column prop="description" label="描述"/>
      <el-table-column fixed="right" label="操作" width="350px">
        <template #default="scope">
          <el-button type="info" :icon="CirclePlus" @click="MenuAdd(scope.row.id)" v-if="!scope.row.pid">新建菜单
          </el-button>
          <el-button type="primary" :icon="Edit" @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-popconfirm title="确定要删除吗?" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button :icon="Remove" type="danger">删除</el-button>
            </template>
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-dialog
          v-model="dialogVisible"
          title="提示"
          width="30%"
      >
        <el-form label-width="120px">
          <el-form-item :model="form" label="菜单名">
            <el-input v-model="form.name" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item :model="form" label="路径">
            <el-input v-model="form.path" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item :model="form" label="图标">
            <el-select clearable v-model="form.icon" placeholder="请选择" style="width: 100%">
              <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.value">
                <i :class="item.value" /> {{ item.name }}
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :model="form" label="描述">
            <el-input v-model="form.description" style="width: 80%"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </span>
        </template>
      </el-dialog>
    </div>
  </el-card>
</div>
</template>
<script>
// @ is an alias to /src
import request from "@/utils/request";
import { Edit,Remove,CirclePlus,Upload,Download,Search,Refresh,View,CircleClose,Menu } from '@element-plus/icons-vue';
export default {
  setup(){
    return{
      Edit,
      Remove,
      CirclePlus,
      Upload,
      Download,
      Search,
      Refresh,
      View,
      CircleClose,
      Menu
    }
  },
  name: 'Role',
  components: {Edit,Menu},
  data() {
    return {
      form: {},
      dialogVisible: false,
      currentPage: 1,
      total: 10,
      pageSize: 10,
      name:"",
      tableData: [],
      isShow:false,
      ids:[],
      options:[]
    }
  },
  created() {
    this.load();
  },
  methods: {
    MenuAdd(pid){
      this.dialogVisible=true;
      this.form={};
      if(pid){
        this.form.pid=pid
      }
    },
    tableRowStyle({ row, rowIndex }) {
      return { background:'#F2F6FC', color: '#0052d9', 'font-weight': 600 }
    },
    reset(){
      this.name="";
      this.load()
    },
    Show(){
      this.isShow=!this.isShow
    },
    deleteBatch(){
      if(!this.ids.length){
        this.$message.warning("请选择数据!")
        return
      }
      request.post("/menu/del/batch",this.ids).then(res=>{
        if(res){
          this.$message.success("批量删除成功")
          this.load()
        }else{
          this.$message.error("批量删除失败")
        }
      })
    },
    handleSelectionChange(val){
      console.log(this.ids)
      this.ids=val.map(v => v.id)
    },
    handleUploadSuccess(res) {
      console.log(res)
      if (res.code === '200') {
        this.$message.success("导入成功")
        this.load()
      }
    },
    exportRole() {
      location.href = "http://localhost:9090/menu/export";
    },

    showBooks(books){
      this.bookList=books;
      this.bookVis=true
    },

    load() {
      //,{
    //   params:{
    //     name:this.name
    //   }
    // }
      request.get("/menu").then(res => {
        console.log(res)
        this.tableData = res.data;
      })
    },
    add() {
      this.dialogVisible = true;
      this.form = {}
    },
    save() {
      if (this.form.id) {
        request.post("/menu/add", this.form).then(res => {
          console.log(res);
          if (res.code === '200') {
            this.$message({
              type: "success",
              message: "更新成功"
            })
          } else {
            this.$message({
              type: "error",
              message: "更新失败"
            })
          }
          this.load();
          this.dialogVisible = false
        });
      } else {
        request.post("/menu/add", this.form).then(res => {
          console.log(res);
          if (res.code === '200') {
            this.$message({
              type: "success",
              message: "新增成功"
            })
          } else {
            this.$message({
              type: "error",
              message: "新增失败"
            })
          }
          this.load();
          this.dialogVisible = false
        })
      }
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
      request.get("/menu/icons").then(res => {
        console.log(this.options)
        this.options = res.data;
      })
    },
    handleSizeChange(pageSize) {
      this.pageSize=pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.currentPage=pageNum;
      this.load();
    },
    handleDelete(id){
      request.delete("/menu/"+id).then(res => {
        console.log(res.code)
        if (res){
          this.$message({
            type: "success",
            message: "删除成功"
          })
        }else {
          this.$message({
            type: "error",
            message: "删除失败"
          })
        }
        this.load()//重新加载
      });
    },

  }
}
</script>
